package com.ktgj.flight.common.airStationPrint;


import com.alibaba.fastjson.JSON;
import com.ktgj.common.core.utils.HttpClientUtil;
import com.ktgj.common.core.utils.SpringUtils;
import com.ktgj.common.core.utils.xml.XElement;
import com.ktgj.common.sms.service.CHttpPost;
import com.ktgj.flight.domain.FlightOpenProduct;
import com.ktgj.flight.domain.OkHttpUtil;
import com.ktgj.flight.mapper.FlightOpenProductMapper;
import com.ktgj.sms.api.RemoteSmsService;
import com.ktgj.sms.api.RemoteSmsTemplateService;
import com.ktgj.sms.api.domain.Sms;
import net.bytebuddy.asm.Advice;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class AirStationPrint {

    private static String OFFICEID = "bjs968";
    //private static String ITINERARYURLKEY = "http://itpds.travelsky.com/EtnoAdd.ashx?request={0}&userid={1}&password={2}&ticketNo={3}&fare=&mobilephone=&choice=1";
    private static String ITINERARYURLKEY = "https://itp.travelsky.com/EtnoAdd.ashx";
    private static String USERID="";
    private static String USERPASS= "";

    @Autowired
    FlightOpenProductMapper flightOpenProductMapper;
    @Autowired
    private RemoteSmsTemplateService remoteSmsTemplateService;
    @Autowired
    private RemoteSmsService remoteSmsService;

    public AirStationPrint()
    {
        flightOpenProductMapper = SpringUtils.getBean(FlightOpenProductMapper.class);
        FlightOpenProduct queryData = new FlightOpenProduct();
        List<FlightOpenProduct> lstFlightProduct = flightOpenProductMapper.selectFlightOpenProductList(queryData);
        FlightOpenProduct flightOpenProduct =  lstFlightProduct.stream().filter(item -> item.getTpId() == 7).findFirst().orElse(null);
        if (flightOpenProduct!=null)
        {
            this.USERID = flightOpenProduct.getUserName();
            this.USERPASS = flightOpenProduct.getUserPwd();
        }
    }


    /**
     * 添加票号到信天游行程单中
     * @param ticketNo
     * @return
     */
    public String AddTicketNo(String ticketNo, String phone)
    {
        String format = String.format("?userid=%s&password=%s&ticketNo=%s&fare=&mobilephone=&choice=1", USERID, USERPASS, ticketNo);
        String URLKEY = ITINERARYURLKEY + format;
        Response response = OkHttpUtil.postJsonParam(URLKEY, "");
        String params = null;
        try {
            params = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                response.close();
            }
        }
        if (response.code() == 200) {
//            String ip = "";
//            String port = "";
//            String strUserId = "";
//            String strPwd = "";
//            String strMobiles = "";
//            String strMessage = "";
//            String strSubPort = "";
//            String strUserMsgId = "";
//            try {
//                //String url = ResourceUtils.getURL("classpath:config.xml").getPath();
//                String url = this.getClass().getClassLoader().getResource("config.xml").getPath();
//                XElement root = XElement.load(url);
//                XElement element = root.element("channelOne");
//                ip = element.getAttributeValue("ip");
//                port = element.getAttributeValue("port");
//                strUserId = element.getAttributeValue("userId");
//                strPwd = element.getAttributeValue("userPass");
//                strSubPort = element.getAttributeValue("SubPort");
//                //以上为短信发送的基本配置
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
            Sms sms = new Sms();
            //获取到短信模板
            String smsTemplate = remoteSmsTemplateService.getTemplate(2l).getData().getTemplateContent();
            //编辑模板生成待发送短信内容
            smsTemplate = smsTemplate.replaceFirst("#\\{Code}", ticketNo);
            sms.setMobile(phone);
            sms.setSmsContent(smsTemplate);
            remoteSmsService.sendSmsMessage(sms);
            //发送短信
//            strMobiles = phone;
//            strMessage = smsTemplate;
//            strUserMsgId = "202108101611";
//            CHttpPost cHttpPost = new CHttpPost();
//            StringBuffer stringBuffer = new StringBuffer();
//            cHttpPost.SendSms(stringBuffer, ip, port, strUserId, strPwd, strMobiles, strMessage, strSubPort, strUserMsgId, null, null, null, null, null, null, null, null, null);
        }
        return params;
    }

}
